package com.ddyx.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddyx.entity.wxFootprint;
import com.ddyx.mapper.wxFootprintMapper;
import com.ddyx.service.IWxFootprintService;
import com.ddyx.util.DateUtil;
import com.ddyx.util.Page;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class WxFootprintServiceImpl implements IWxFootprintService{

	@Resource
	wxFootprintMapper footprintMapper;
	
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm selectwxFootprintsByUid(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String,Object> paramMap = (Map<String,Object>)param.getData();
		try {
			List<wxFootprint> footprintList= footprintMapper.selectwxFootprintsByUid(Page.parse(paramMap));
			result.setData(footprintList);
			int total = footprintMapper.selectFootprintCount(paramMap);
			result.setTotal(total);
		} catch (Exception e) {
			result.setStatus(false);
			result.setMessage("查询商品详情错误");
			log.error(e+"查询商品详情错误");
		}
		return result;
	}

	@Transactional
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm insertwxFootprint(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String,Object> paramMap = (Map<String,Object>)param.getData();
		try {
			String actionAready = paramMap.get("actionAready")+"";
			if(actionAready.equals("0")) {
				int isExist = footprintMapper.checkIsExitsFootprint(paramMap);
				if(isExist > 0) {
					String actionType = paramMap.get("actionType")+"";
					switch (actionType) {
//					case "0":
//						result.setMessage("您已经喜欢过该商品了");
//						break;
					case "1":
						result.setMessage("您已经收藏过该商品了");
						break;
					case "2":
						result.setMessage("您已经点赞过该商品了");
						break;
					default:
						result.setStatus(false);
						result.setMessage("事件类型错误");
						break;
					}
				}else {
					paramMap.put("createTime", DateUtil.format_ymd_hms.format(new Date()));
					int index = footprintMapper.insertwxFootprint(paramMap);
					if(index < 0) {
						result.setStatus(false);
						result.setMessage("加入足迹信息失败");
					}
				}
			}else {
				footprintMapper.updatewxFootprint(paramMap);
			}
			
		} catch (Exception e) {
			result.setStatus(false);
			result.setMessage("未知错误");
			log.error(e+"未知错误");
		}
		return result;
	}
	
	
//	@SuppressWarnings("unchecked")
//	@Override
//	public ResponseForm selectwxFootprints(RequestForm param) {
//		ResponseForm result = new ResponseForm();
//		Map<String,Object> paramMap = (Map<String,Object>)param.getData();
//		try {
//			List<wxFootprint> footprintList= footprintMapper.selectwxFootprintsByUid(paramMap);
//			result.setData(footprintList);
//		} catch (Exception e) {
//			result.setStatus(false);
//			result.setMessage("查询商品详情错误");
//			log.error(e+"查询商品详情错误");
//		}
//		return result;
//	}

}
